package top.shen33.yang.base;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import top.shen33.yang.base.job.ChickenJob;
import top.shen33.yang.model.vo.QuartzEntity;
import top.shen33.yang.services.QrtzJobDetailsService;

import java.io.File;

@Component
public class AfterStartRunner  implements ApplicationRunner{


    private final static Logger LOGGER = LoggerFactory.getLogger(AfterStartRunner.class);

    @Autowired
    private YmlConfig ymlConfig;


    @Autowired
    private QrtzJobDetailsService jobService;
    @Autowired
    private Scheduler scheduler;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        String cfgPath = ymlConfig.getCfgPath();
        File file = new File(cfgPath, "file.json");
        if (!file.exists()) {
            file.mkdirs();
        }



//        https://gitee.com/52itstyle/spring-boot-task.git

//        Integer count = jobService.total(null);
//        if (count == 0) {
//            LOGGER.info("初始化测试任务");
//            QuartzEntity quartz = new QuartzEntity();
//            quartz.setJobName("test01");
//            quartz.setJobGroup("test");
//            quartz.setDescription("测试任务");
//            quartz.setJobClassName(ChickenJob.class.getName());
//            quartz.setCronExpression("0/20 * * * * ?");
//            Class cls = Class.forName(quartz.getJobClassName());
//            cls.newInstance();
//
//            //构建job信息
//            JobDetail job = JobBuilder.newJob(cls).withIdentity(quartz.getJobName(),
//                    quartz.getJobGroup())
//                    .withDescription(quartz.getDescription()).build();
//
//
//            //添加JobDataMap数据
//            job.getJobDataMap().put("itstyle", "科帮网欢迎你");
//            job.getJobDataMap().put("blog", "https://blog.52itstyle.com");
//            job.getJobDataMap().put("data", new String[]{"张三", "李四"});
//            // 触发时间点
//            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCronExpression());
//            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger" + quartz.getJobName(), quartz.getJobGroup())
//                    .startNow().withSchedule(cronScheduleBuilder).build();
//
//
//            //交由Scheduler安排触发
//            scheduler.scheduleJob(job, trigger);
//        }
    }
}
